//
//  MenuViewController.swift
//  iModern
//
//  Created by Ralf on 5/27/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class MenuViewController: TableViewController
{
    var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let items  = ["邀请好友", "我的购买", "我的销售", "我的商品", "我的动态", "登出"]
    let images = ["SM-Invite.png", "SM-Appointment.png", "SM-Order", "SM-Advertising.png", "SM-Article.png", "Button-Background-Orange.png"]
    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.navigationItem.title = "设置"

        self.tableView.registerNib(UINib(nibName: "MenuViewLinkCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "LINK")

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        self.initBackButton()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return items.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("LINK", forIndexPath: indexPath) as! MenuViewLinkCell
        if (indexPath.row < items.count - 1)
        {
            cell.setData(indexPath.row, text: items[indexPath.row], image: images[indexPath.row])
        }
        else
        {
            //            cell.setData(indexPath.row, text: items[indexPath.row], image:images[indexPath.row],cellType: "Logout")
            let logout = UIButton(frame: CGRectMake(50, 85, cell.bounds.size.width - 100, 30))
            logout.setBackgroundImage(UIImage(named: "Button-Background-Orange.png"), forState: UIControlState.Normal)
            logout.titleLabel?.font = UIFont.systemFontOfSize(14)
            logout.setTitle("登 出", forState: UIControlState.Normal)
            logout.addTarget(self, action: "logout:", forControlEvents: UIControlEvents.TouchUpInside)
            cell.addSubview(logout)
            cell.accessoryType = .None
        }
        return cell
    }

    @IBAction func logout(sender: AnyObject)
    {
        appDelegate.contactManager.logout()
        NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
        NSNotificationCenter.defaultCenter().postNotificationName("Refresh Shop", object: nil)

        self.navigationController?.popToRootViewControllerAnimated(true)
    }
    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        switch indexPath.row
        {
        case 0:
            let controller = InviteViewController()
            self.pushViewController(controller)
        case 1:
            let controller = MyAppointmentViewController()
            self.pushViewController(controller)
        case 2:
            let controller = MyOrderViewController()
            self.pushViewController(controller)
        case 3:
            let controller = MyAdvertisingViewController()
            self.pushViewController(controller)
        case 4:
            let controller = MyArticleViewController()


            self.pushViewController(controller)

        default:
            break
        }
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        if indexPath.row >= 5
        {
            return 200.0
        }
        else
        {
            return 60
        }
    }
}
